/*
 * KasiskiAttack.h
 *
 *  Created on: 23/11/2011
 *      Author: gisedaye
 */

#ifndef KASISKIATTACK_H_
#define KASISKIATTACK_H_
#define PATTERNLENGHT 3

#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <string>
#include <iostream>
#include <vector>
#include "../../util/Utils.h"

using namespace std;

class CharacterPositions{
	private:
		string character;
		int position1;
		int position2;
	public:
		CharacterPositions(string chara,int pos1, int pos2){
			this->character=chara;
			this->position1=pos1;
			this->position2=pos2;
		}
		string getCharacter(){
			return this->character;
		}
		int getPosition1(){
			return this->position1;
		}
		int getPosition2(){
			return this->position2;
		}
};

class KasiskiAttack {
private:
	char * text;
	char * filetext;
	long textlen;
	long minlen;
	int keyLenght;
	vector<int> distances;
	vector<int> distanceRepeat;
	vector<CharacterPositions *> repeatedCharacters;

public:
	KasiskiAttack();
	virtual ~KasiskiAttack();
	void attack(string filename);
	void readFile(string filename);
	void analizeWords();
	void error(long a, string c);
	long greatCommonDivisor(long a, long b);
	void findKeyLenght();
	void addDistance(int dist);
	void breakVigenere();
	bool inArray(char a, vector<char> vec, int * pos);
	char keyCharacter(vector<char>*letras,vector<int> * repeticiones);
	string decipher(string keyword);

};

#endif /* KASISKIATTACK_H_ */
